﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AnesSystem.ViewModels;
using AnesSystem.Models;

namespace AnesSystem.Controls
{
    /// <summary>
    /// CommonPageContent.xaml 的交互逻辑
    /// </summary>
    public partial class CommonPageContent : UserControl
    {
        #region Variables

        private MenuListModel _selectedSecondLevelMenu;
        private MenuListModel _selectedThreeMenu;

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="nextLevelMenu"></param>
        public CommonPageContent(MenuListModel secondLevelMenu)
        {
            InitializeComponent();
            _secondMenuListCtrl.VM.Click += SecondMenuCtrl_Click;
            InitialContent(secondLevelMenu);
            this.Loaded += CommonPageContent_Loaded;
        }

        #endregion

        #region Properties



        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods



        #endregion

        #region Public/Protected Methods

        public void InitialContent(MenuListModel menu)
        {
            _selectedSecondLevelMenu = menu;
            IEnumerable<MenuListModel> list = DataManager.Instance.MenuList.Where(O => O.Index == menu.Parent);
            MenuListModel pMenu = list.FirstOrDefault();
            if (menu.Fxid.Equals("wenshu") || (pMenu != null && pMenu.Fxid.Equals("wenshu")))
            {
                _leftMenuCtrl.Visibility = Visibility.Visible;

                if (pMenu != null && pMenu.Fxid.Equals("wenshu"))
                {
                    _selectedThreeMenu = menu;
                    _selectedSecondLevelMenu = pMenu;
                }
                else if (_selectedThreeMenu == null)
                {
                    _selectedThreeMenu = DataManager.Instance.MenuList.Where(O => O.Parent == _selectedSecondLevelMenu.Index).OrderBy(O => O.Order).FirstOrDefault(); ;
                }
                _leftMenuCtrl.InitialMenuListCtrl(_selectedSecondLevelMenu.Index, _selectedThreeMenu.ClassName);
                ChangeContent(_selectedThreeMenu.ClassName);
                (_leftMenuCtrl.DataContext as VerticalMenuCtrlViewModel).Click -= CommonPageContent_Click;
                (_leftMenuCtrl.DataContext as VerticalMenuCtrlViewModel).Click += CommonPageContent_Click;
                _secondMenuListCtrl.VM.SetSelected(_selectedSecondLevelMenu);
            }
            else
            {
                _leftMenuCtrl.Visibility = Visibility.Collapsed;
                if (_selectedSecondLevelMenu.MenuType == 0)
                {
                    MenuListModel model = DataManager.Instance.MenuList.FirstOrDefault(O => O.Parent == _selectedSecondLevelMenu.Index && O.IsDefaultOpen);

                    ChangeContent(model==null?"":model.ClassName);
                }
                else
                {
                    ChangeContent(_selectedSecondLevelMenu.ClassName);
                }
            }
            
        }

        public void ChangeContent(string className)
        {
            _rightContent.ViewModel.CurrentRightContent = DataManager.Instance.GetFrameworkElement(className);
        }

        #endregion

        #region EventHandlers

        private void CommonPageContent_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void SecondMenuCtrl_Click(object sender, MenuListModel e)
        {
            _selectedSecondLevelMenu = e;
            InitialContent(e);
        }

        private void CommonPageContent_Click(object sender, MenuListModel e)
        {
            _selectedThreeMenu = e;
            ChangeContent(e.ClassName);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //NavigateWin.Navigation.Navigate(FirstPage.Instance);
            NavigateWin.Navigation.Back();
        }

        #endregion

    }
}
